Use PUN2 instead of PUN for multiplayer in Unity
Overview
Photon is the first choice when creating multiplayer games in Unity. The SDK for Unity is Photon Unity Networking (PUN), but there are two types of PUN, PUN, which has been around for a long time, and PUN2, which was released around 2018. So, when I try to use Photon, most of the articles that come out when I search for “Unity Photon” are articles of PUN, and even if I search with “PUN2”, only a few Japanese articles are hit. After touching PUN for a year and a half at work, touching PUN2 for a little less than half a year, isn’t there any merit in using PUN anymore? That’s why I’m talking about using PUN2.
What is Photon in the first place? ?? ??
For the time being
Very simply, it is a real-time communication SaaS for games. Even though it is called Photon, it is subdivided into Real Time, Bolt, Chat, Voice, etc. PUN is the SDK that integrates them and is provided for Unity.
It can be implemented without being aware of the back end, and the charge is proportional to the upper limit of simultaneous connection, so it is easy for individual developers to use, which is the first reason to be mentioned when making multiplayer with Unity. Also, personally, it is a high point that you can communicate with UDP as standard and complement the coordinates of the frame between communications. Their implementation is too heavy …
This is amazing PUN2
Significantly enhanced compared to PUN
- The API has become quite cool
- Instantiate can now be customized
- The connection has been strengthened
I will explain in order.
1. API became cool
To be honest, there is a thing that PUN was too cool. As anyone who has touched it will understand, the naming convention of members is completely out of the MS standard, and the callback system has a dynamic processing-like cousin, so it’s not like “C #”. It was a library, right? PUN. It’s a lot like C #. It seems that the naming convention has become quite common, and most of the callbacks are called via Interface, which improves performance.
2. Instantiate can now be customized
For those who have never used PUN, PUN creates Prefabs with the * PhotonNetworking.Instantiate * method (* Instantiate *) to synchronize objects with other players. However, in the case of PUN, this method is the songwriter, and the generated Prefab can only be specified by reading it from Resources. That Resources, that Unity officially says Don’t use it. It seems that it is not impossible to read from another place if you do your best, but I failed once and gave up.
However, in Gicchon PUN2, it is now possible to easily customize the Prefab loading method inside * Instantiate * by setting the * IPunPrefabPool * implementation class. For example, you can specify the GameObject referenced in SerializeField and Prefab in AssetBundle with a little effort. As a result, performance and asset management are dramatically improved compared to PUN.
3. Connection has been strengthened
This is the biggest. In PUN, if the application is out of focus for a short time (feeling a little over a second), the connection will be cut and it will be treated as disconnected. This out-of-focus often occurs with smartphone apps, for example, just by displaying the “Battery is n% remaining” dialog. In other words, when using PUN with a smartphone app, the return process was almost indispensable, especially for games with strong real-time characteristics. (By the way, the reconnection function of PUN is only “you can reconnect the connection”, so if you want to return to the game, you need to implement data restoration etc. by yourself)
However, PUN2 enhances this connection. Specifically, it doesn’t cut even if the focus is lost for a little over 5 seconds. At this point, it will not happen unless you intentionally minimize the application or put the main unit to sleep, so it is a category that you can treat it as disconnected by specification and play it from the game. However, I don’t know why & how it was enhanced … It’s not officially written (Photon isn’t so rich in Doc in the first place), and it’s hard to read the implementation around Socket communication. …… If anyone knows, please let me know …
This is a disadvantage PUN2
It’s not just a delicious story. As I wrote in the overview, most of Photon’s articles falling in the ocean of the Internet are from PUN, that is, the sample code that is falling is also from PUN. As mentioned above, in PUN2, the naming and callback mechanism have changed, so just copying and pasting the sample code will not compile. Even if I pass, I can’t get a callback. However, conversely, this is the only disadvantage of using PUN2 instead of PUN.
Summary
If you are going to make a multiplayer game with Unity from now on, I recommend you to use PUN2 if you are not confident in coding and you are not worried about even modifying the sample code. Also, People who have summarized the changes and responses in terms of code and People who have summarized the migration method from PUN synamon.hatenablog.com/entry/2019/04/18/193751) There are also people, so if you look at this area, I think that even if you are not confident in coding, you will not have much trouble.
However, in the first place, it is quite difficult to use Photon for development of a certain scale or more. Even at work, I use a wrapper class with over 1000 lines to add various functions. It’s a popular Fall Guys implemented in Unity + MultiPlay. Isn’t it good to try? I will conclude with irresponsible remarks, the end.